'use strict';

/**
 * Created by zhu on 14/12/26.
 */
angular.module('comment').controller('CommentCtrl', [
    '$scope',
    '$routeParams',
    '$cookies',
    'RouteIdService',
    'CommentQueryService',
    'CommentModifyService',
    'PathSuffixConstant',
    'PageConstant',
    'ProfilePhotoService',
    'PhotoHost',
    function ($scope, $routeParams, $cookies, RouteIdService, CommentQueryService, CommentModifyService, PathSuffixConstant, PageConstant, ProfilePhotoService, PhotoHost) {
        var author = {identity: $cookies.userid, name: $cookies.username};
        var hostId = RouteIdService.get();

        $scope.newComment = {content: ''};

        /**
         * 加载所有的评论
         */
        $scope.query = function (pageNumber) {
            if (!hostId) {
                return;
            }

            var queryCondition = {
                offset: (pageNumber - 1) * PageConstant.countPerPage,
                countPerPage: PageConstant.countPerPage,
                type: 'blog',
                hostId: hostId
            };

            CommentQueryService.query(queryCondition, function (response) {
                $scope.comments = response.results || [];
                $scope.totalCount = response.totalCount;
                angular.forEach($scope.comments, function (comment) {
                    comment.comments = comment.comments || [];
                });
                setPhotosUri($scope.comments);
            });
        };

        function setPhotosUri(objectList) {
            angular.forEach(objectList, function (object) {
                ProfilePhotoService.query({userId: object.author.identity}, function (response) {
                    object.author.photoUri = PhotoHost + 'cdrmimg/' + response.photoId + '?w=50&h=50';
                });
                if (object.comments) {
                    setPhotosUri(object.comments);
                }
            });
        }

        this.query = function (id) {
            if (id) {
                hostId = id;
            }
            $scope.query();
        };

        /**
         * 添加一个新的评论
         */
        $scope.addComment = function (commentType) {
            if ($scope.newComment.content == '') {
                alert('请录入评论内容！');
                return;
            }
            $scope.newComment.referenceId = hostId;
            $scope.newComment.author = author;
            $scope.newComment.commentType = commentType;
            $scope.newComment.createTime = {'$date': new Date()}

            CommentModifyService.add($scope.newComment, function (response) {
                var createdComment = $scope.newComment;
                createdComment._id = response._id;
                ProfilePhotoService.query({userId: createdComment.author.identity}, function (response) {
                    createdComment.author.photoUri = PhotoHost + 'cdrmimg/' + response.photoId + '?w=50&h=50';
                });
                $scope.comments.push(createdComment);
                $scope.blog.replies += 1;
                $scope.newComment = '';
            });
        };

        /**
         * 需要回复
         *
         * @param comment
         */
        $scope.openReply = function (comment) {
            comment.needReply = true;
        };

        /**
         * 关闭回复
         *
         * @param comment
         */
        $scope.closeReply = function (comment) {
            comment.needReply = false;
            comment.newReply = '';
        };

        /**
         * 对评论增加一个回复
         *
         * @param comment
         */
        $scope.addReply = function (comment, host) {
            var referenceId = host._id;
            if (!comment.newReply || comment.newReply == '') {
                alert('请录入回复内容！');
                return;
            }

            var newReply = {
                referenceId: referenceId,
                author: {identity: $cookies.userid},
                commentType: 'blog',
                content: comment.newReply,
                object: comment.author
            };

            CommentModifyService.add(newReply, function (response) {
                newReply._id = response._id;
                newReply.author.name = $cookies.username;
                newReply.createTime = {$date: new Date()};
                ProfilePhotoService.query({userId: newReply.author.identity}, function (response) {
                    newReply.author.photoUri = PhotoHost + 'cdrmimg/' + response.photoId + '?w=50&h=50';
                });
                host.comments = host.comments || [];
                host.comments.push(newReply);
                comment.newReply = '';
                $scope.closeReply(comment);
            });
        };

        $scope.query(1);
    }]);
